home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / Resources / CutePDF 2.3 / converter.exe / GNUGS / VIEWJPEG.PS < prev    next >
Text File  |  2002-02-23  |  5KB  |  153 lines

  1. %! viewjpeg.ps   Copyright (C) 1994 Thomas Merz <tm@pdflib.com>
  2. %
  3. % This software is provided AS-IS with no warranty, either express or
  4. % implied.
  5. % This software is distributed under license and may not be copied,
  6. % modified or distributed except as expressly authorized under the terms
  7. % of the license contained in the file LICENSE in this distribution.
  8. % For more information about licensing, please refer to
  9. % http://www.ghostscript.com/licensing/. For information on
  10. % commercial licensing, go to http://www.artifex.com/licensing/ or
  11. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  12. % San Rafael, CA  94903, U.S.A., +1(415)492-9861.
  13.  
  14. % $Id: viewjpeg.ps,v 1.1.6.2 2002/02/22 19:45:55 ray Exp $
  15. % View JPEG files with Ghostscript
  16. %
  17. % This PostScript code relies on level 2 features.
  18. %
  19. % Only JPEG baseline, extended sequential, and progressive files
  20. % are supported.  Note that Adobe PostScript level 2 does not include
  21. % progressive-JPEG support.  Ghostscript with IJG JPEG v6 or later
  22. % will decode progressive JPEG, but only if you edit gsjmorec.h to
  23. % enable that feature.
  24. %
  25. % Updated by L. Peter Deutsch 20-May-1997:
  26. %   move the usage example to the beginning
  27. % Updates by Tom Lane 6-Sep-1995
  28.  
  29. % Usage example:
  30. %    (jpeg-6/testimg.jpg) viewJPEG
  31.  
  32. /languagelevel where {pop languagelevel 2 lt}{true} ifelse {
  33.   (JPEG needs PostScript Level 2!\n) print flush stop
  34. } if
  35.  
  36. /JPEGdict 20 dict def
  37. JPEGdict begin
  38.  
  39. /NoParamMarkers [    % JPEG markers without additional parameters
  40.     16#D0 16#D1 16#D2 16#D3 16#D4 16#D5 16#D6 16#D7 16#D8 16#01
  41. ] def
  42.  
  43. /NotSupportedMarkers [     % JPEG markers not supported by PostScript level 2
  44.     16#C3 16#C5 16#C6 16#C7 16#C8 16#C9 16#CA 16#CB 16#CD 16#CE 16#CF
  45. ] def
  46.  
  47. % Names of color spaces
  48. /ColorSpaceNames << /1 /DeviceGray /3 /DeviceRGB /4 /DeviceCMYK >> def
  49.  
  50. % read one byte from file F
  51. % - ==> int --or-- stop context
  52. /NextByte { 
  53.     F read not { (Read error in ViewJPEG!\n) print flush stop } if
  54. } bind def
  55.  
  56. /SkipSegment {    % read two bytes and skip that much data
  57.     NextByte 8 bitshift NextByte add 2 sub { NextByte pop } repeat
  58. } bind def
  59.  
  60. % read width, height, and # of components from JPEG markers
  61. % and store in dict
  62. /readJPEGmarkers {    % - ==> dict --or-- stop context
  63.     5 dict begin
  64.  
  65.     { % loop: read JPEG marker segments until we find SOFn marker or EOF
  66.     NextByte
  67.     16#FF eq {                % found marker
  68.         /markertype NextByte def
  69.         % Is it S0F0=baseline, SOF1=extended sequential, SOF2=progressive ?
  70.         markertype dup 16#C0 ge exch 16#C2 le and {
  71.         NextByte pop NextByte pop    % segment length
  72.         % Ghostscript and Adobe PS accept only data precision 8
  73.         NextByte 8 ne {
  74.             (Error: not 8 bits per component!\n) print flush stop 
  75.         } if
  76.  
  77.         % Read crucial image parameters
  78.         /height NextByte 8 bitshift NextByte add def
  79.         /width NextByte 8 bitshift NextByte add def
  80.         /colors NextByte def
  81.  
  82.         DEBUG { currentdict { exch == == } forall flush } if
  83.         exit
  84.         } if
  85.  
  86.         % detect several segment types which are not compatible with PS
  87.         NotSupportedMarkers {
  88.         markertype eq {
  89.             (Marker ) print markertype == 
  90.             (not supported!\n) print flush stop
  91.         } if 
  92.         } forall 
  93.  
  94.         % Skip segment if marker has parameters associated with it
  95.         true NoParamMarkers { markertype eq {pop false exit} if } forall 
  96.         { SkipSegment } if
  97.     } if
  98.     } loop
  99.  
  100.     currentdict dup /markertype undef
  101.     end
  102. } bind def
  103.  
  104. end    % JPEGdict
  105.  
  106. % read image parameters from JPEG file and display the image
  107. /viewJPEG {        % <file|string> ==> -
  108.     save 
  109.     JPEGdict begin
  110.     /saved exch def
  111.     /scratch 1 string def
  112.     dup type /stringtype eq { (r) file } if
  113.     /F exch def
  114.  
  115.     readJPEGmarkers begin
  116.     F 0 setfileposition        % reset file pointer
  117.  
  118.     % We use the whole clipping area for the image (at least in one dimension)
  119.     gsave clippath pathbbox grestore
  120.     /ury exch def /urx exch def
  121.     /lly exch def /llx exch def
  122.  
  123.     llx lly translate
  124.     width height scale
  125.  
  126.     % use whole width or height, whichever is appropriate
  127.     urx llx sub width div ury lly sub height div
  128.     2 copy gt { exch } if pop        % min
  129.     dup scale
  130.     ColorSpaceNames colors scratch cvs get setcolorspace
  131.  
  132.     % prepare image dictionary
  133.     << /ImageType 1
  134.        /Width width
  135.        /Height height
  136.        /ImageMatrix [ width 0 0 height neg 0 height ]
  137.        /BitsPerComponent 8
  138.        % If 4-component (CMYK), assume data is inverted per Adobe Photoshop
  139.        colors 4 eq {
  140.          /Decode [ colors { 1 0 } repeat ]
  141.        } {
  142.          /Decode [ colors { 0 1 } repeat ]
  143.        } ifelse
  144.        /DataSource F /DCTDecode filter
  145.     >> image
  146.  
  147.     end        % image parameter dictionary
  148.  
  149.     saved end restore
  150. } bind def
  151.